package com.dianshang.controller.goods;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dianshang.entity.PageResult;
import com.dianshang.entity.Result;
import com.dianshang.pojo.goods.Goods;
import com.dianshang.pojo.goods.Spu;
import com.dianshang.service.goods.SpuService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/spu")
public class SpuController {

    @Reference
    private SpuService spuService;

    /**
     * 查询所有
     */
    @GetMapping("/findAll")
    public List<Spu> findAll(){
        return spuService.findAll();
    }

    /**
     * 查询所有+分页
     */
    @GetMapping("/findPage")
    public PageResult<Spu> findPage(Integer page, Integer size){
        return spuService.findPage(page, size);
    }

    /**
     * 查询所有+模糊
     */
    @PostMapping("/findList")
    public List<Spu> findList(@RequestBody Map<String,Object> searchMap){
        return spuService.findList(searchMap);
    }

    /**
     * 查询所有+分页+模糊
     */
    @PostMapping("/findPage")
    public PageResult<Spu> findPage(@RequestBody Map<String,Object> searchMap,Integer page, Integer size){
        return  spuService.findPage(searchMap,page,size);
    }

    /**
     * 回显
     */
    @GetMapping("/findById")
    public Spu findById(Integer id){
        return spuService.findById(id);
    }

    /**
     * 添加
     */
    @PostMapping("/add")
    public Result add(@RequestBody Spu spu){
        spuService.add(spu);
        return new Result();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result update(@RequestBody Spu spu){
        spuService.update(spu);
        return new Result();
    }

    /**
     * 删除
     */
    @GetMapping("/delete")
    public Result delete(Integer id){
        spuService.delete(id);
        return new Result();
    }

    /**
     * 添加Goods表
     */
    @PostMapping("/saveGoods")
    public Result saveGoods(@RequestBody Goods goods){
        spuService.saveGoods(goods);
        return new Result();
    }

    /**
     * 查询商品组合
     */
    @GetMapping("/findGoodsById")
    public Goods findGoodsById(String id){
        return spuService.findGoodsById(id);
    }

    /**
     * 商品审核
     */
    @GetMapping("/audit")
    public Result audit(String id,String status){
        spuService.audit(id,status);
        return new Result();
    }

    /**
     * 商品批量审核通过
     */
    // 方法一(商品批量审核通过):
    /* @PostMapping("/auditMany")
    public Result auditMany(@RequestBody List<Spu> spus){
        spuService.auditMany(spus);
        return new Result();
    } */
    // 方法二(商品批量审核通过):
    @PostMapping("/auditMany")
    public Result auditMany(@RequestBody String[] ids){
        int count = spuService.auditMany(ids);
        return new Result(0,"审核通过了"+count+"个商品");
    }

    /**
     * 下架商品
     */
    @GetMapping("/pull")
    public Result pull(String id){
        spuService.pull(id);
        return new Result();
    }

    /**
     * 上架商品
     */
    @GetMapping("/put")
    public Result put(String id){
        spuService.put(id);
        return new Result();
    }

    /**
     * 批量上架商品
     */
    @PostMapping("/putMany")
    public Result putMany(@RequestBody String [] ids){
        int count = spuService.putMany(ids);
        return new Result(0,"上架"+count+"个商品");
    }

    /**
     * 批量下架商品
     */
    @PostMapping("/pullMany")
    public Result pullMany(@RequestBody String [] ids){
        int count = spuService.pullMany(ids);
        return new Result(0,"下架"+count+"个商品");
    }

    /**
     * 逻辑删除
     */
    @GetMapping("/deleteGoods")
    public Result deleteGoods(String id){
        spuService.deleteGoods(id);
        return new Result();
    }

    /**
     * 物理删除
     */
    @GetMapping("/removeGoods")
    @Transactional
    public Result removeGoods(String id){
        spuService.removeGoods(id);
        return new Result();
    }

    /**
     * 还原逻辑删除
     */
    @GetMapping("/revertGoods")
    public Result revertGoods(String id){
        spuService.revertGoods(id);
        return new Result();
    }

}